package com.example.company.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.company.entity.ApiResult;
import com.example.company.entity.ApiResultHandler;
import com.example.company.entity.Result;
import com.example.company.entity.Users;
import com.example.company.mapper.UsersMapper;
import com.example.company.service.IUsersService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 存储系统用户信息 前端控制器
 * </p>
 *
 * @author 88Ra
 * @since 2024-12-31
 */

@RestController
@RequestMapping("/api")
public class UsersController {
	
	@Autowired
	private IUsersService iusersService;
	
	@Autowired
	private UsersMapper usersMapper;
	
	
	/**
	 * 注册
	 *
	 * @param users
	 * @return
	 */
	@CrossOrigin
	@PostMapping("/users/employee/register")
	public ApiResult RegisterUser(@RequestBody Users users) {
		int count = usersMapper.insert(users);
		if (count == 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", users);
			
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
	}
	
	
	/**
	 * 员工，部门管理者都可以添加
	 *
	 * @param users
	 * @return
	 */
	@CrossOrigin
	@PostMapping("/users/employee/login")
	public ApiResult LoginUser(@RequestBody Users users) {
		Users res = usersMapper.selectOne(Wrappers.<Users>lambdaQuery()
				.eq(Users::getUsername, users.getUsername())
				.eq(Users::getPassword, users.getPassword()));
		if (res != null) {
			
			
			return ApiResultHandler.buildApiResult(200, "请求成功", res);
			
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
	}
	
	
//	@GetMapping("/users/list")
//	public ApiResult findrole() {
//		List<Users> users = new ArrayList<>();
//		users = usersMapper.selectByRole();
//		if (users.size() >= 1) {
//			return ApiResultHandler.buildApiResult(200, "请求成功", users);
//		} else {
//			return ApiResultHandler.buildApiResult(400, "请求失败", null);
//
//		}
//	}
	
	
	@GetMapping("/users/department/select")
	public ApiResult finddepartment() {
		List<Users> users = new ArrayList<>();
		users = usersMapper.selectDepartment();
		if (users.size() >= 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", users);
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
	}
	
	
	@GetMapping("/users/list")
	public PageInfo<Users> findPage(@RequestParam(defaultValue = "1") Integer pageNum
			, @RequestParam(defaultValue = "10") Integer pageSize,  Users users) {
		
		PageInfo<Users> pageInfo=iusersService.findPage(pageNum, pageSize, users);
		return pageInfo;
	}
	
	
	
	@GetMapping("/users/list/{departmentid}")
	public PageInfo<Users> findPage2(@RequestParam(defaultValue = "1") Integer pageNum
			, @RequestParam(defaultValue = "10") Integer pageSize,  Users users,
			                         @PathVariable("departmentid") Integer departmentid) {
		
		PageInfo<Users> pageInfo=iusersService.findPage2(pageNum, pageSize, users,departmentid);
		return pageInfo;
	}
	/**
	 * 删除杨工信息
	 *
	 * @param userids
	 * @return
	 */
	@DeleteMapping("users/delete/{userids}")
	
	public ApiResult deleteusers(@PathVariable Long[] userids) {
		
		int cur = iusersService.deleteUsersByUserIDs(userids);
		
		if (cur >= 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", cur);
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
	}
	
	/**
	 * 查询用户信息
	 *
	 * @param userid
	 * @return
	 */
	@GetMapping(value = "/users/{userid}")
	public Users getInfo(@PathVariable("userid") int userid) {
		Users users=iusersService.selectUsersByUserID(userid);
		return users;
	}
	
	/**
	 * 修改用户信息
	 *
	 * @param users
	 * @return
	 */
	@PutMapping("users")
	public ApiResult edit(@RequestBody Users users) {
		int cur = iusersService.updateUsers(users);
		if (cur >= 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", cur);
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
		
		
	}
	
	
	
	
}
